﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CHR.Contract.Common;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.SQL;
using Kingdee.BOS;
using System.Data;
using Kingdee.BOS.Orm.DataEntity;

namespace CHR.App.Core.Common
{
    public class NameRepeatService:INameRepeat
    {
        public bool NameRepeat(Context ctx, LocaleValue names, string tableName, string pkField, long pkValue, Dictionary<string, object> whereParams)
        {
            string strSql = @"SELECT SUM(C) FROM ({0}) tab";
            if (names == null||names.Count==0)
            {
                return false;
            }
            List<string> innerSqlList = new List<string>();
            List<SqlParam> paParams=new List<SqlParam>();
            foreach (KeyValuePair<Int32, string> name in names)
            {
                if(name.Value.Trim().Length==0)
                {
                    continue;
                }
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(string.Format("SELECT COUNT(1) AS C from {0} a inner join {0}_L b on a.{1}=b.{1}",
                                            tableName, pkField));

                sb.AppendLine(string.Format(" where fname=@fname_{0} ", name.Key));
                paParams.Add(new SqlParam(string.Format("@fname_{0}",name.Key),DbType.String,name.Value));
                sb.AppendLine(string.Format(" and FLOCALEID={0} ", name.Key));
         

                foreach (var w in whereParams)
                {
                    Type T = w.Value.GetType();
                    switch (T.Name.ToUpperInvariant())
                    {
                        case "CHAR":
                        case "STRING":
                            sb.AppendLine(string.Format(" and {0}='{1}' ", w.Key, w.Value));
                            break;
                        default:
                            sb.AppendLine(string.Format(" and {0}={1} ", w.Key, w.Value));
                            break;

                    }

                }
                sb.AppendLine(string.Format(" and a.{0}<>{1}", pkField, pkValue));
                innerSqlList.Add(sb.ToString());
            }
            if(innerSqlList.Count==0)
            {
                return false;
            }
            strSql = string.Format(strSql, string.Join(" union all ",innerSqlList));

            return DBUtils.ExecuteScalar<int>(ctx, strSql, 0, paParams.ToArray()) > 0;
        }

        /// <summary>
        ///获取重名的语言码
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="names"></param>
        /// <param name="tableName"></param>
        /// <param name="pkField"></param>
        /// <param name="pkValue"></param>
        /// <param name="whereParams"></param>
        /// <returns></returns>
        public DynamicObjectCollection GetNameRepeatLocale(Context ctx, LocaleValue names, string tableName, string pkField, long pkValue, Dictionary<string, object> whereParams)
        {
            string strSql = @"{0}";
            if (names == null || names.Count == 0)
            {
                return null;
            }
            List<string> innerSqlList = new List<string>();
            List<SqlParam> paParams = new List<SqlParam>();
            foreach (KeyValuePair<Int32, string> name in names)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(string.Format("SELECT FLOCALEID from {0} a inner join {0}_L b on a.{1}=b.{1}",
                                            tableName, pkField));

                sb.AppendLine(string.Format(" where fname=@fname_{0} ", name.Key));
                paParams.Add(new SqlParam(string.Format("@fname_{0}", name.Key), DbType.String, name.Value));
                sb.AppendLine(string.Format(" and FLOCALEID={0} ", name.Key));


                foreach (var w in whereParams)
                {
                    Type T = w.Value.GetType();
                    switch (T.Name.ToUpperInvariant())
                    {
                        case "CHAR":
                        case "STRING":
                            sb.AppendLine(string.Format(" and {0}='{1}' ", w.Key, w.Value));
                            break;
                        default:
                            sb.AppendLine(string.Format(" and {0}={1} ", w.Key, w.Value));
                            break;

                    }

                }
                sb.AppendLine(string.Format(" and a.{0}<>{1}", pkField, pkValue));
                innerSqlList.Add(sb.ToString());
            }
            strSql = string.Format(strSql, string.Join(" union all ", innerSqlList));

            return DBUtils.ExecuteDynamicObject(ctx, strSql, null, null, CommandType.Text, paParams.ToArray());
        }
    }
}
